﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using csSMC6X;
using System.Text.RegularExpressions;


namespace ChangIP
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            //Timer1 = new Timer();
            //Timer1.Interval = 200;
            //Timer1.Tick = new EventHandler()
        }

        IntPtr m_handle;  //连接标识

        private String confirmIP(String ipStr)
        {
            if(ipStr=="" || ipStr == null)
            {
                MessageBox.Show("请输入IP。");
                return null;
            }else
            {
                if (!isIPaddr(ipStr))
                {
                    return null;
                }
            }
            return ipStr;
        }
        
        private bool isIPaddr(String ipStr)
        {
            bool blnTest = false;
            bool _Result = true;

            Regex regex = new Regex("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$");
            blnTest = regex.IsMatch(ipStr);
            if (blnTest == true)
            {
                string[] strTemp = ipStr.Split(new char[] { '.' }); 
                for (int i = 0; i < strTemp.Length; i++)
                {
                    if (Convert.ToInt32(strTemp[i]) > 255)
                    { //大于255则提示，不符合IP格式 
                        MessageBox.Show("IP格式不正确。");
                        _Result = false;
                    }
                }
            }
            else
            {
                //输入非数字则提示，不符合IP格式 
                MessageBox.Show("IP格式不正确。");
                _Result = false;
            }
            return _Result;
        }


        private void changeVisibles(bool isConnect)
        {
            this.gateLabel.Visible          = isConnect;
            this.modifyButton.Visible       = isConnect;
            this.modifyGateTextBox.Visible  = isConnect;
            this.modifyIpLabel.Visible      = isConnect;
            this.modifyMastTextBox.Visible  = isConnect;
            this.marskLabel.Visible         = isConnect;
            this.modifyIpTextBox.Visible    = isConnect;
            this.LinkEth.Enabled            = !isConnect;
            this.connectIpTextBox.Enabled   = !isConnect;
            if (isConnect)
            {
                this.Text = "连接成功";
            }else
            {
                this.Text = "IN3 Change IP Tool"; 
            }
        }

        private void OnLink(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            int iret =-1;
            if (btn.Name == "LinkEth")
            {
                //string ipStr = "192.168.1.11";
                string ipStr = confirmIP(connectIpTextBox.Text);
                if(ipStr== null)
                {
                    return;
                }
                this.Text = "正在连接网络...";
                iret = SMC6X.SMCOpenEth(ipStr, ref m_handle); //连接网络   
             
            }
            else if (btn.Name == "LinkCom")
            {
                this.Text = "正在连接串口...";
                iret = SMC6X.SMCOpenCom(1, ref m_handle); //连接网络
            }
            
            if (0 != iret )
            {
                this.Text = "连接失败";
                MessageBox.Show("连接失败");
                return;
            }

            changeVisibles(true);
            //timer2.Start();
        }

        private void PMove_Click(object sender, EventArgs e)
        {
            if (m_handle == (IntPtr)(0))
            {
                MessageBox.Show("请先连接控制器！");
                return;
            }
            Button btn = (Button)sender;
            SMC6X.SMCSetLocateSpeed(m_handle, 0, 1000); //设置运动速度，1000 Pulse/s;
            SMC6X.SMCSetLocateAcceleration(m_handle, 0, 2000); //设置运动加速度，2000 Pulse/s2;
           if (btn.Name == ("PMovePlus"))
           {
               SMC6X.SMCPMovePluses(m_handle, 0, 50000, 0);  //向正方向运动50000个脉冲；
           }
           else if (btn.Name == ("PMoveReverse"))
           {
               SMC6X.SMCPMovePluses(m_handle, 0, -50000, 0);  //向负方向运动50000个脉冲；
           }
            
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            SMC6X.SMCDecelStop(m_handle,0);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            m_handle = (IntPtr)(0);       
        }


        private void OnTick(object sender, EventArgs e)
        {
            //Int32 pos = 0;
            //SMC6X.SMCGetPositionPulses(m_handle, 0, ref pos);
         }

        private void CloseConnect_Click(object sender, EventArgs e)
        {
            SMC6X.SMCClose(m_handle);
            m_handle = (IntPtr)(0);

            changeVisibles(false);
        }

        private String confirmMarsk(String marskStr)
        {
            //////////////////need confirm///////////////////////
            return marskStr;
        }

        private String confirmGate(String gate ,string ip, string marsk)
        {
            //////////////////////need confirm //////////////////////////
            return gate;
        }
        
        private void modifyButton_Click(object sender, EventArgs e)
        {
            String ip = confirmIP(this.modifyIpTextBox.Text).Replace('.',',');;

            if (ip!= null)
            {
                String marsk = confirmMarsk(this.modifyMastTextBox.Text).Replace('.',',');;
                if (marsk!=null)
                {
                    String gate = confirmGate(this.modifyGateTextBox.Text, ip, marsk).Replace('.',',');;
                    if (gate!=null)
                    {
                        byte[] reip = new byte[100];

                        int cip = SMC6X.SMCCommand(m_handle, "SETIP(" + ip + ")", ref reip[0], 100);
                        Console.Write("CIP = {0:d}", cip);
                        cip |= SMC6X.SMCCommand(m_handle, "SETMAKE(" + marsk + ")", ref reip[0], 100);
                        Console.Write("CIP = {0:d}", cip);
                        cip |= SMC6X.SMCCommand(m_handle, "SETGATE(" + gate + ")", ref reip[0], 100);
                        Console.Write("CIP = {0:d}", cip);
                        cip |= SMC6X.SMCCommand(m_handle, "BURNSET", ref reip[0], 100);
                        Console.Write("CIP = {0:d}", cip);

                        if(cip== 0)
                        {
                            MessageBox.Show("修改成功!");
                        }else
                        {
                            MessageBox.Show("修改失败。");
                        }
                    }
                }
            }
        }

    }
}
